package com.leetcode.algorithm.y22.m08.w3;

import java.util.LinkedList;
import java.util.Queue;

import com.leetcode.algorithm.common.TreeNode;

/**
 * 513. 找树左下角的值
 * 
 * https://leetcode.cn/problems/find-bottom-left-tree-value/
 * 
 * @author jie.deng
 *
 */
class Question0513Solution01 {

	public int findBottomLeftValue(TreeNode root) {
		// BFS
		int num = 0;
		Queue<TreeNode> queue = new LinkedList<>();
		queue.offer(root);
		while (!queue.isEmpty()) {
			int len = queue.size();
			num = queue.peek().val;
			for (int i = 0; i < len; i++) {
				TreeNode node = queue.poll();
				if (node.left != null) {
					queue.offer(node.left);
				}
				if (node.right != null) {
					queue.offer(node.right);
				}
			}
		}
		return num;
	}

}